package core;

import org.kxml2.kdom.Element;
import org.kxml2.kdom.Node;

public class DisciplineType{
	
	private int m_typeNumber;
	private static final String m_types[] = {"T1", "T2", "T3"};

	public DisciplineType(int typeNumber){
		setType(typeNumber);
	}
	
	public void setType(int typeNumber){
		if(typeNumber >= m_types.length)
			m_typeNumber = m_types.length - 1;
		else if(typeNumber < 0)
			m_typeNumber = 0;
		else
			m_typeNumber = typeNumber;
	}
	
	public String getTypeName(){
		return m_types[m_typeNumber];
	}
	
	public int getTypeKey(){
		return m_typeNumber;
	}
	
	public static String[] getNames(){
		return m_types;
	}

	public void toXml(Node node){
		Element disciplTypeNode = node.createElement(null, "DisciplineType");
		disciplTypeNode.setAttribute(null, "number", new Integer(m_typeNumber).toString());		
		node.addChild(Node.ELEMENT, disciplTypeNode);        
	}

	public static DisciplineType fromXml(Element elem) throws Throwable{
		try
	    {
			Element rootElem = elem; 
			if (!rootElem.getName().equals("DisciplineType")){
				rootElem = elem.getElement(null, "DisciplineType");
			}
			
			int num = Integer.parseInt(rootElem.getAttributeValue(null, "number"));
			return new DisciplineType(num);
	    }
		catch (Exception e) {
			throw e;
		}
	}
}

